
import 'package:flutter_getx/app/base/state_layout.dart';
import 'package:flutter_getx/app/config/constant.dart';
import 'package:get/get_rx/src/rx_types/rx_types.dart';

import 'base_state.dart';

class BaseListState<T> extends BaseState {

  final list = <T>[].obs;
  final hasMore = true.obs;

  void setHasMore(bool _hasMore) {
    hasMore.value = _hasMore;
  }

  void add(T data) {
    list.add(data);
  }

  void addAll(List<T> data) {
    list.addAll(data);
  }

  void addDatas(List<T>? data, bool refresh, {StateType emptyType = StateType.empty, int pageSize = Constant.pageSize}) {
    setHasMore(!(data == null || data.isEmpty || data.length < pageSize));
    if(refresh){
      clear();
      if(data == null || data.isEmpty){
        stateType.value = emptyType;
      }
    }
    if(null != data) {
      list.addAll(data);
    }
  }

  void insert(int i, T data) {
    list.insert(i, data);
  }

  void insertAll(int i, List<T> data) {
    list.insertAll(i, data);
  }

  void remove(T data) {
    list.remove(data);
  }

  void removeAt(int i) {
    list.removeAt(i);
  }

  void clear() {
    list.clear();
  }

}
